<?php
namespace App\Service\Admin\Platform;

use Illuminate\Support\Facades\DB;

class AccountService2 extends BaseService
{
    //账户

    /**
     * 列表
     * @param array $pas 【phone手机号】
     * @param array $pages【start，limit】
     */
    public function getIndex($pas, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbUser)->where('del', 0);
        if ($pas['phone']) {
            $db = $db->where('phone', $pas['phone']);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'realname' => $v['realname'],
                'phone' => $v['phone'],
                'lang_front' => $v['lang_front'],
                'lang_back' => $v['lang_back'],
                'uname' => $v['uname'],
                'user_limit' => $v['user_limit'],
                'period' => $v['period'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    //选项
    public function getOption()
    {
        $arys = ['front' => [], 'back' => [], 'try_day' => [], 'period' => []];
        //前端语言
        $arys['front'] = $this->dbConn($this->dbMkConfig)
            ->select('id', 'title')
            ->where('del', 0)
            ->where('type', 2)
            ->get();
        //后端语言
        $arys['back'] = $this->dbConn($this->dbMkConfig)
            ->select('id', 'title')
            ->where('del', 0)
            ->where('type', 3)
            ->get();
        //试用时间
        $dayTypes = $this->userPeriodDayTypes();
        $days = [];
        foreach ($dayTypes as $k => $v) {
            $days[] = [
                'id' => $k,
                'title' => $v,
            ];
        }
        $arys['try_day'] = $days;
        //有效时间
        $arys['period'] = $days;
        return $arys;
    }

    /**
     * 账号添加
     * @param array $pas
     */
    public function add($pas)
    {
        $db = $this->dbConn($this->dbUser)->where('del', 0);
        $res = (array) $db->where('realname', $pas['realname'])->first();
        if (!empty($res)) {
            return $this->toback(-2, '已存在同姓名账号');
        }
        $res = (array) $db->where('uname', $pas['uname'])->first();
        if (!empty($res)) {
            return $this->toback(-2, '已存在同名账号');
        }
        $res = (array) $db->where('phone', $pas['phone'])->first();
        if (!empty($res)) {
            return $this->toback(-2, '已存在同手机号账号');
        }
        $pas['passwd'] = md5('123456');
        $pas['addtime'] = toDate();
        $pas['uptime'] = toDate();
        $lastid = $this->dbConn($this->dbUser)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, '添加失败');
        }
        return $this->toback();
    }

    /**
     * 设置拒绝|通过
     * @param int $id 用户ID
     * @param int $auth 2拒绝，3通过
     */
    public function setAuth($id, $auth)
    {
        $res = (array) $this->dbConn($this->dbUser)
            ->where('del', 0)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '账户不存在');
        }
        if ($res['plat_auth'] == 3) {
            return $this->toback(-2, '已审核');
        }
        $upData = [
            'plat_auth' => $auth,
            'authtime' => toDate(),
        ];
        $up = $this->dbConn($this->dbUser)->where('id', $id)->update($upData);
        if (!$up) {
            return $this->toback(-2, '审核失败');
        }
        return $this->toback();
    }

    /**
     * 删除
     * @param int $id 用户ID
     */
    public function setDel($id)
    {
        $res = (array) $this->dbConn($this->dbUser)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '账户不存在');
        }
        $upData = [
            'del' => 1,
            'deltime' => toDate(),
        ];
        $up = $this->dbConn($this->dbUser)->where('id', $id)->update($upData);
        if (!$up) {
            return $this->toback(-2, '删除失败');
        }
        return $this->toback();
    }

    /**
     * 绑卡列表
     * @param string $phone 手机号搜索
     * @param int $auth 1审核中，3已绑定
     */
    public function bankList($phone, $auth = 1)
    {
        $db = $this->dbConn($this->dbBank);
        if ($phone) {
            $uid = $this->dbConn($this->dbUser)->where('phone', $phone)->value('id');
            if (!$uid) {
                return $this->toback(-2, '该手机号用户不存在');
            }
            $db = $db->where('uid', $uid);
        }
        if ($auth) {
            $db = $db->where('is_auth', $auth);
        }
        $total = $db->count();
        $res = $db->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $types = $this->bankTypes;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $uOne = (array) $this->dbConn($this->dbUser)->where('id', $v['uid'])->first();
            $realname = empty($uOne) ? '-' : $uOne['realname'];
            $phone = empty($uOne) ? '-' : $uOne['phone'];
            $type_name = array_key_exists($v['type'], $types) ? $types[$v['type']] : '-';
            $arys[] = [
                'id' => $v['id'],
                'realname' => $realname,
                'phone' => $phone,
                'type' => $v['type'],
                'type_name' => $type_name,
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 绑卡审核
     * @param int $id
     * @param int $auth 1拒绝，2通过
     */
    public function bankAuth($id, $auth)
    {
        $res = (array) $this->dbConn($this->dbBank)
            ->where('is_auth', $auth)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $upData = [
            'is_auth' => $auth== 1 ? 2 : 3,
            'authtime' => toDate(),
        ];
        $up = $this->dbConn($this->dbBank)->where('id', $id)->update($upData);
        if (!$up) {
            return $this->toback(-2, '审核失败');
        }
        return $this->toback();
    }
}